//
//  ChangeMobileVC.m
//  YaoHeAPP
//
//  Created by mac on 15/12/31.
//  Copyright © 2015年 mac. All rights reserved.

//***************************修改手机号

#import "ChangeMobileVC.h"
#import "Masonry.h"
#import "Controller.h"
@interface ChangeMobileVC ()
{
    UIButton*button;
    NSInteger width;
    NSInteger height;
     NSInteger _count;
}
@end

@implementation ChangeMobileVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ChangeMobileVC"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ChangeMobileVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    
    self.view.backgroundColor=bgVCcolor;
    
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];

    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"换绑手机号";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];

    backView=[[UIView alloc]init];
    backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navcBar.mas_bottom).offset(10);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.offset(56);
    }];
    
    //必填星星图标
    UIImageView*startImage1=[[UIImageView alloc]init];
    startImage1.image=[UIImage imageNamed:@"bi_tian"];
    [backView addSubview:startImage1];
    [startImage1 mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(4);
        make.height.offset(12);
        make.bottom.offset(-22);
        make.width.offset(12);
    }];
    //提示标签
    UILabel*phone=[[UILabel alloc]init];
    phone.font=[UIFont systemFontOfSize:15];
    phone.text=@"手机号:";
    [backView addSubview:phone];
    [phone mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(20);
        make.top.offset(6);
        make.bottom.offset(-6);
        make.width.offset(60);
    }];
    
    //手机号输入框
    phoneText=[[UITextField alloc]init];
    phoneText.font=[UIFont systemFontOfSize:14];
    phoneText.placeholder=@"请输入手机号";
    phoneText.borderStyle=UITextBorderStyleNone;
    phoneText.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    [phoneText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [backView addSubview:phoneText];
    [phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phone.mas_right).offset(2);
        make.right.offset(-6);
        make.top.offset(5);
        make.bottom.offset(-5);
    }];
    
    //获取验证码的按钮
    button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.borderWidth=2;
    button.titleLabel.font=[UIFont systemFontOfSize:15];
    button.layer.borderColor=[UIColor orangeColor].CGColor;
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(sendValidation) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-5);
        make.top.offset(8);
        make.bottom.offset(-8);
        make.width.offset(88);
    }];
    
    
    //验证码的背景
    validationView=[[UIView alloc]init];
    validationView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:validationView];
    [validationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_bottom).offset(2);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.offset(56);
    }];
    
    UIImageView*startImage2=[[UIImageView alloc]init];
    startImage2.image=[UIImage imageNamed:@"bi_tian"];
    [validationView addSubview:startImage2];
    [startImage2 mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(4);
        make.height.offset(12);
        make.bottom.offset(-22);
        make.width.offset(12);
    }];

    
    //验证码label
    UILabel*validationLabel=[[UILabel alloc]init];
    validationLabel.font=[UIFont systemFontOfSize:15];
    validationLabel.text=@"验证码:";
    [validationView addSubview:validationLabel];
    [validationLabel mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(20);
        make.top.offset(6);
        make.bottom.offset(-6);
        make.width.offset(60);
    }];
    
    //验证码输入框
    validation=[[UITextField alloc]init];
    validation.clearButtonMode=UITextFieldViewModeWhileEditing;
    validation.font=[UIFont systemFontOfSize:14];
    validation.placeholder=@"请输入验证码:";
    validation.borderStyle=UITextBorderStyleNone;
    [validation addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [validationView addSubview:validation];
    [validation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phone.mas_right).offset(2);
        make.right.offset(-6);
        make.top.offset(5);
        make.bottom.offset(-5);
    }];
    
    //下一步
    UIButton*nextBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"验证后绑定新手机" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [nextBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(validationView.mas_bottom).offset(20);
        make.left.offset(50);
        make.right.offset(-50);
        make.height.offset(40);
    }];
    
    // Do any additional setup after loading the view.
}
//点击按钮
-(void)nextBtn
{
    if (phoneText.text.length!=11) {
        ALERTSHOW(@"请输入正确的手机号");
        return;
    }
    if (validation.text.length<=0) {
        ALERTSHOW(@"请输入验证码");
        return;
    }
    NSDictionary*dic=@{@"MOBILE":phoneText.text,@"CHECK_CODE":validation.text};
    [Controller requestWithURL:@"AppUserCenter/PostSaveRegMobile?from=app" jsondic:dic finishBlock:^(ClassData *clasData) {
        if ([clasData.number integerValue]==0) {
            [_message setValue:phoneText.text forKey:@"regMobile"];
            UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"恭喜你更换绑定手机成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alertView.tag=10;
            [alertView show];
            
            //个人页面直接相应通知
            [[NSNotificationCenter defaultCenter] postNotificationName:@"upname" object:nil];
            
       }
        else{
            ALERTSHOW(clasData.message);
        }
    }];
    
}

//警告框的协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


//返回上一层
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
//点击按钮发送验证码
-(void)sendValidation
{
    if (phoneText.text.length==11)
    {
      
        NSDictionary*dic=@{@"MOBILE":phoneText.text};
        
        [Controller requestWithURL:@"AppUserCenter/PostSendChangeMobileCheckCode?from=app" jsondic:dic finishBlock:^(ClassData*data) {
            if ([data.number integerValue]==0){
                button.enabled =NO;
                _count = 60;
                [button setTitle:@"60秒" forState:UIControlStateDisabled];
                [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
            }
            if ([data.number integerValue]==1)
            {
                ALERTSHOW(data.message);
            }
        }];
        return;
    }
     ALERTSHOW(@"手机号输入不正确");
}


//定时器的方法
-(void)timerFired:(NSTimer *)timer
{
    if (_count !=1) {
        _count -=1;
        [button setTitle:[NSString stringWithFormat:@"%ld秒",(long)_count] forState:UIControlStateDisabled];
    }
    else
    {
        [timer invalidate];
        button.enabled = YES;
        [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

//键盘弹下
-(void)keyboardDissmiss
{
    return;
}
//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
